package cn.aresoft.framework.commons;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.aresoft.framework.util.Page;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;

/**
 * 泛型Ibatis Dao基类 提供分页函数和若干便捷查询方法，并对返回值作了泛型类型转
 * 
 * @author Jerry 2009-7-10 上午09:57:44
 * @param <T>
 */
@SuppressWarnings("unchecked")
public class IbatisGenericDao extends SqlMapClientDaoSupport {
	private SqlExecutor sqlExecutor;
 
	/**
	 * @return the sqlExecutor
	 */
	public SqlExecutor getSqlExecutor() {
		return sqlExecutor;
	}

	/**
	 * @param sqlExecutor
	 *            the sqlExecutor to set
	 */
	public void setSqlExecutor(SqlExecutor sqlExecutor) {
		this.sqlExecutor = sqlExecutor;
	}

	public void initialize() throws Exception {
		if (sqlExecutor != null) {
			SqlMapClient sqlMapClient = getSqlMapClientTemplate()
					.getSqlMapClient();
			if (sqlMapClient instanceof ExtendedSqlMapClient) {
				ReflectUtil.setFieldValue(((ExtendedSqlMapClient) sqlMapClient)
						.getDelegate(), "sqlExecutor", SqlExecutor.class,
						sqlExecutor);
			}
		}
	}

	public Page findForList(String sqlName, Serializable arg, int start,
			int pageSize) {
		logger.debug("准备查询..."+sqlName);
		List rs_list = null;
		if (arg == null) {
			rs_list = getSqlMapClientTemplate().queryForList(sqlName, start,
					pageSize);
		} else {
			rs_list = getSqlMapClientTemplate().queryForList(sqlName, arg,
					start, pageSize);
		}
		long totalSize = tableCountNum(sqlName + "Count", arg);
		return new Page(start, totalSize, pageSize, rs_list);
	}

	public List findForList(String sqlName, Serializable arg) {
		logger.debug("准备查询..."+sqlName);
		List rs_list = null;
		if (arg == null) {
			rs_list = getSqlMapClientTemplate().queryForList(sqlName);
		} else {
			rs_list = getSqlMapClientTemplate().queryForList(sqlName,arg);
		}
		return rs_list;
	}
	
	public List findForSubList(String sqlName, Serializable arg, int begin, int offset){
		
		// TODO Auto-generated method stub
		logger.debug("准备查询..."+sqlName);
		List rs_list = getSqlMapClientTemplate().queryForList(sqlName, arg, begin, offset);
		logger.debug("查询结束!");
		return rs_list;
	}
	

	public Object insert(String insertSqlName, Serializable obj) {
		// TODO Auto-generated method stub
		logger.debug("正在插入数据..."+insertSqlName);
		Object obj1 = getSqlMapClientTemplate().insert(insertSqlName, obj);
		logger.debug("插入数据结束!");
		return obj1;
	}

	public Object load(String sqlName, Serializable pk) {
		// TODO Auto-generated method stub
		logger.debug("正在加载数据..."+sqlName);
		Object obj1 = getSqlMapClientTemplate().queryForObject(sqlName, pk);
		logger.debug("加载数据结束!");
		return obj1;
	}

	public int remove(String removeSqlName, Serializable obj) {
		// TODO Auto-generated method stub
		logger.debug("正在删除数据..."+removeSqlName);
		int rs = getSqlMapClientTemplate().delete(removeSqlName, obj);
		logger.debug("删除数据结束!");
		return rs;
	}

	public int update(String updateSqlName, Serializable obj) {
		// TODO Auto-generated method stub
		logger.debug("正在更新数据..."+updateSqlName);
		int rs = getSqlMapClientTemplate().update(updateSqlName, obj);
		logger.debug("更新数据结束!");
		return rs;
	}

	public int tableCountNum(String sqlName, Serializable obj) {
		// TODO Auto-generated method stub
		logger.debug("正在统计数据..."+sqlName);
		int rs = ((Integer) getObject(sqlName, obj)).intValue();
		logger.debug("统计数据结束!");
		return rs;
	}

	public Object getObject(String sqlName, Serializable obj) {
		// TODO Auto-generated method stub
		logger.debug("正在加载数据..."+sqlName);
		return getSqlMapClientTemplate().queryForObject(sqlName, obj);
	}
	public Object getObjectInfo(String sqlName, Object obj) {
		// TODO Auto-generated method stub
		logger.debug("正在加载数据..."+sqlName);
		return getSqlMapClientTemplate().queryForObject(sqlName, obj);
	}
}
